/******************************************************************************/
/*  Class SMARTBUG                                                            */
/*  PO: S. Maslyakov, rusoil.9@gmail.com                                      */
/*                                                                            */
/*  Revision:     1.0                                                         */
/*  Date:         2010/08/26 00:13:33                                         */
/******************************************************************************/

#include "data_types.h"

#include "Core\core.h"
#include "Device\device.h"
#include "Spi\spi.h"
#include "Ain\ain.h"
#include "Din\din.h"
#include "Relay\relay.h"
#include "Led\led.h"
#include "Heater\heater.h"
#include "Enet\enet.h"
#include "Temp\temp.h"
#include "Usb\usb.h"
#include "Rtc\rtc.h"
#include "At25df\at25df.h"
#include "Rfile\rfile.h"
#include "Mlog\mlog.h"
#include "Action\action.h"
#include "Updater\updater.h"
#include "Mb_master\mb_master.h"
#include "Processor\processor.h"
#include "Setpoint\setpoint.h"
#include "Packet\packet.h"

#include "Uart\uart0.h"

#include "Modbus\mb.h"
#include "Modbus\rtu\mbrtu.h"
#include "Modbus\port\port.h"
#include "Modbus\mbport.h"

#include "Modem\modem.h"

#include "TNKernel\tn.h"
#include "smartbug.h"



//==============================================================================
// App: Init hw
//==============================================================================
void SMARTBUG::Init_Device() {

    // Dis unused hw modules
    PCONP_bit.PCUART0 = 0;
    PCONP_bit.PCPWM1 = 0;
    PCONP_bit.PCI2C0 = 0;
    PCONP_bit.PCSSP1 = 0;
    PCONP_bit.PCAN1 = 0;
    PCONP_bit.PCAN2 = 0;
    PCONP_bit.PCI2C1 = 0;
    PCONP_bit.PCSSP0 = 0;
    PCONP_bit.PCTIM2 = 0;
    PCONP_bit.PCTIM3 = 0;
    PCONP_bit.PCUART2 = 0;
    PCONP_bit.PCI2C2 = 0;
    PCONP_bit.PCI2S = 0;
    PCONP_bit.PCSDC = 0;
    PCONP_bit.PCGPDMA = 0;
    PCONP_bit.PCENET = 0;

    InitIrq();
    SPI::Init_Spi();
    UART0::Init_Uart(115200, 3);

    // Init program modules
    AIN::Init_Ain();
    DIN::Init_Din();
    RELAY::Init_Relay();
    LED::Init_Led();
    HEATER::Init_Heater();
    ENET::Init_Enet();
    PROCESSOR::Init_Processor();
    SETPOINT::Init_Setpoint();
    RFILE::Init_Rfile();
    RTC::Init_Rtc();
    PACKET::Init_Packet();
    MLOG::Init_Mlog();
    MB_MASTER::Init_MbMaster();
    AT25DF::Init_At25df();
    ACTION::Init_Action();
    UPDATER::Init_Updater();
    TEMP::Init_Temp();
    USB::Init_Usb();
    DEVICE::Init_Device();
    MODEM::Init_Modem();

    // Run wdt
    CORE::Init_Wdt();
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Init interrupts
//==============================================================================
void SMARTBUG::InitIrq() {
    VICPROTECTION_bit.VIC_ACCESS = 0;

    // Init VIC
    uint32_t *vect_addr, *vect_cntl;

    VICINTENCLEAR = 0xffffffff;
    VICADDRESS = 0UL;
    VICINTSELECT = 0UL;

    for (uint32_t i = 0; i < VIC_SIZE; i++) {
        vect_addr = (uint32_t *)(VIC_BASE_ADDR + VECT_ADDR_INDEX + i*4);
        vect_cntl = (uint32_t *)(VIC_BASE_ADDR + VECT_CNTL_INDEX + i*4);
        *vect_addr = 0x0;
        *vect_cntl = 0xF;
    };

    // Configure used int
    VICINTSELECT_bit.TIMER0 = 0;          // Int from TIMER0 = IRQ
    VICVECTPRIORITY4_bit.PRIORITY = 4;    // Priority int from TIMER0

    VICINTSELECT_bit.TIMER1 = 0;          // Int from TIMER1 = IRQ
    VICVECTPRIORITY5_bit.PRIORITY = 2;    // Priority int from TIMER1

    VICINTSELECT_bit.TIMER2 = 0;          // Int from TIMER2 = IRQ
    VICVECTPRIORITY26_bit.PRIORITY = 3;   // Priority int from TIMER2

    VICINTSELECT_bit.TIMER3 = 0;          // Int from TIMER3 = IRQ
    VICVECTPRIORITY27_bit.PRIORITY = 5;   // Priority int from TIMER3

    VICINTSELECT_bit.UART1 = 0;           // Int from UART1 = IRQ
    VICVECTPRIORITY7_bit.PRIORITY = 9;    // Priority int from UART1

    VICINTSELECT_bit.UART2 = 0;           // Int from UART2 = IRQ
    VICVECTPRIORITY28_bit.PRIORITY = 9;   // Priority int from UART2

    VICINTSELECT_bit.USB = 0;             // Int from USB = IRQ
    VICVECTPRIORITY22_bit.PRIORITY = 2;   // Priority int from USB

    // Addr handlers
    VICVECTADDR4 = (uint32_t)InterruptHandlerTimer0;
    VICVECTADDR5 = (uint32_t)InterruptHandlerTimer1;
    VICVECTADDR26 = (uint32_t)InterruptHandlerTimer2;
    VICVECTADDR27 = (uint32_t)InterruptHandlerTimer3;
    VICVECTADDR7 = (uint32_t)MODEM::InterruptHandlerUart1;
    VICVECTADDR28 = (uint32_t)MB_MASTER::InterruptHandlerUart2;
    VICVECTADDR22 = (uint32_t)USB::InterruptHandlerUsb;

    // Enable used int
    VICINTENABLE_bit.TIMER0 = 1;
    VICINTENABLE_bit.TIMER1 = 1;
    VICINTENABLE_bit.TIMER2 = 1;
    VICINTENABLE_bit.TIMER3 = 1;
    VICINTENABLE_bit.UART1 = 1;
    VICINTENABLE_bit.UART2 = 1;
    VICINTENABLE_bit.USB = 1;
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Int handler for TIMER0
//==============================================================================
void SMARTBUG::InterruptHandlerTimer0() {
    tn_tick_int_processing();

    T0IR = 0x01;
    VICADDRESS = 0;
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Int handler for TIMER1
//==============================================================================
void SMARTBUG::InterruptHandlerTimer1() {
    tn_event_iset((TN_EVENT *)DS1820::Get_ds1820_event(), DS1820_DELAY_EVENT);

    T1IR = 0x01;
    VICADDRESS = 0;
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Int handler for TIMER2
//==============================================================================
void SMARTBUG::InterruptHandlerTimer2() {

    // Modbus-slave timer
    xMbRtuTimerT35Expired();

    T2IR = 0x01;
    VICADDRESS = 0;
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Int handler for TIMER3
//==============================================================================
void SMARTBUG::InterruptHandlerTimer3() {
    // Device routine
    DEVICE::TimerTickRoutine();

    // Modem timeout routine
    MODEM::ProcessTimeout();

    // Packet routine
    PACKET::TimerRoutine();

    T3IR = 0x01;
    VICADDRESS = 0;
}
//==============================================================================
//==============================================================================


//==============================================================================
// App: Init tasks for TNKernel
//==============================================================================
void SMARTBUG::Tn_app_init() {

    // Proc task
    PROCESSOR::Init_proc_task();

    // Cond task
    SETPOINT::Init_setpoint_task();

    // Packet
    PACKET::Init_packet_task();

    // Updater task
    UPDATER::Init_updater_task();

    // Relay task
    RELAY::Init_relay_task();

    // Din task
    DIN::Init_din_task();

    // Ain task
    AIN::Init_ain_task();

    // Led task
    LED::Init_led_task();

    // Heater task
    HEATER::Init_heater_task();

    // Temp task
    TEMP::Init_temp_task();

    // Modem tasks
    MODEM::Init_modem_task();

    // Modbus master tasks
    MB_MASTER::Init_mb_task();

    // Modbus tasks
    Init_modbus_tasks();

    // Rfile tasks
    RFILE::Init_rfile_tasks();

    // Mlog tasks
    MLOG::Init_mlog_tasks();

    // Event_log write_event_task
    ACTION::Init_paction_task();

    // Device write sett task
    DEVICE::Init_device_task();

    // Round-robin
    //tn_sys_tslice_ticks(1, 3);
}
//==============================================================================
//==============================================================================